package com.qf.flash.ui.mine;

import android.app.Dialog;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hyphenate.chat.EMClient;
import com.qf.dblib.dao.entity.UserInfo;
import com.qf.dblib.dao.impl.UserMgr;
import com.qf.flash.R;
import com.qf.flash.event.UserUpdateEvent;
import com.qf.flash.model.BalanceBean;
import com.qf.flash.mvp.contract.AccountDetailContract;
import com.qf.flash.mvp.contract.UserInfoContract;
import com.qf.flash.mvp.presenter.UserInfoPresenter;
import com.qf.flash.ui.login.LoginRegisterActivity;
import com.qf.flash.utils.ImageLoader;
import com.qf.flashlib.base.LazyBaseFragment;
import com.qf.flashlib.widget.ViewUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建日期：2020/2/22 on 20:27
 * 描述:
 * 作者:戴旭东
 */
public class MineFragment extends LazyBaseFragment implements UserInfoContract.View, AccountDetailContract.View {
    @BindView(R.id.iv_set)
    ImageView ivSet;
    @BindView(R.id.rl_top)
    RelativeLayout rlTop;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.progress_h)
    ProgressBar progressH;
    @BindView(R.id.ll_edit_information)
    LinearLayout llEditInformation;
    @BindView(R.id.rl_head)
    RelativeLayout rlHead;
    @BindView(R.id.ll_launch_act)
    LinearLayout llLaunchAct;
    @BindView(R.id.ll_participate_act)
    LinearLayout llParticipateAct;
    @BindView(R.id.ll_collect_store)
    LinearLayout llCollectStore;
    @BindView(R.id.ll_recommend)
    LinearLayout llRecommend;
    @BindView(R.id.ll_chang_pwd)
    LinearLayout llChangPwd;
    @BindView(R.id.ll_coupon)
    LinearLayout llCoupon;
    @BindView(R.id.ll_business)
    LinearLayout llBusiness;
    @BindView(R.id.iv_head_circle)
    ImageView ivHeadCircle;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_level)
    TextView tvLevel;
    @BindView(R.id.tv_id)
    TextView tvId;
    @BindView(R.id.tv_mvp)
    TextView tvMvp;
    @BindView(R.id.tv_yq)
    TextView tvYq;
    @BindView(R.id.tv_zh)
    TextView tvZh;
    @BindView(R.id.tv_yz)
    TextView tvYz;
    @BindView(R.id.tv_progress)
    TextView tvProgress;
    @BindView(R.id.tv_djd)
    TextView tvDjd;
    @BindView(R.id.mRefresh)
    SmartRefreshLayout mRefresh;

    private ProgressBar progressBar;
    private UserInfo loginUser;
    private UserInfoPresenter presenter;

    @Override
    protected int setContentView() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initView() {
        loginUser = UserMgr.getInstance().getLoginUser();
        progressBar = getContentView().findViewById(R.id.progress_h);
    }

    @Override
    protected void lazyLoad() {
        //圆形头像
        setUi();
        presenter.getUserInfo(loginUser.getUid());
        mRefresh.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);
        mRefresh.setOnRefreshListener(refreshLayout -> presenter.getUserInfo(loginUser.getUid()));
    }

    @Override
    protected void initInject() {
        super.initInject();
        presenter = new UserInfoPresenter();
        presenter.attachView(this);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.detachView();
    }

    @Override
    protected boolean useEvent() {
        return true;
    }

    private void setUi() {
        ImageLoader.loadCircleHead(ivHeadCircle.getContext(), loginUser.getPhoto(), ivHeadCircle);
        tvName.setText(loginUser.getName());
        tvLevel.setText("LV." + loginUser.getLevel());
        tvId.setText("ID: " + loginUser.getUid());
        tvMvp.setText(loginUser.getMvpCount() + "局");
        tvYq.setText(loginUser.getYqCount() + "局");
        tvZh.setText(loginUser.getZhCount() + "局");
        tvYz.setText(loginUser.getYzCount() + "局");
        progressBar.setProgress(loginUser.getNextProgress());
        tvProgress.setText(loginUser.getNextProgress() + "%");
        tvDjd.setText(loginUser.getAccountMoney() + "");
        if ("5".equals(loginUser.getUserType())) {
            llBusiness.setVisibility(View.VISIBLE);
        } else {
            llBusiness.setVisibility(View.GONE);
        }
    }


    @OnClick({R.id.iv_head_circle, R.id.iv_set, R.id.ll_edit_information, R.id.ll_launch_act, R.id.ll_participate_act, R.id.ll_collect_store, R.id.ll_recommend, R.id.ll_chang_pwd, R.id.ll_coupon, R.id.ll_business})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_set://显示dialog
                final Dialog dialog = new Dialog(getContext(), R.style.custom_dialog2);
                Window window = dialog.getWindow();
                window.setGravity(Gravity.BOTTOM);
                window.setWindowAnimations(R.style.BottomInAndOutStyle);
                View v = View.inflate(getContext(), R.layout.dialog_set, null);
                window.setContentView(v);
                window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);//设置横向全屏
                dialog.show();
                v.findViewById(R.id.tv_logout).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog.dismiss();
                        UserMgr.getInstance().exitLogin();
                        EMClient.getInstance().logout(true);
                        LoginRegisterActivity.start(getActivity(), true);
                        FragmentActivity activity = getActivity();
                        if (activity != null)
                            activity.finish();
                    }
                });
                v.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog.dismiss();
                    }
                });
                break;
            case R.id.iv_head_circle:
            case R.id.ll_edit_information://编辑资料
                EditInformationActivity.start(getActivity(), loginUser);
                break;
            case R.id.ll_launch_act://我发起的
                CommonRecyclerActivity.start(getActivity(), 1);
                break;
            case R.id.ll_participate_act:// 我参与的
                CommonRecyclerActivity.start(getActivity(), 2);
                break;
            case R.id.ll_collect_store://   我收藏的商家
                MyCollectedShopActivity.start(getActivity());
                break;
            case R.id.ll_recommend://我的推荐
                MyRecommendActivity.start(getActivity(), 1);
                break;
            case R.id.ll_chang_pwd://修改密码
                ChangPasswordActivity.start(getActivity(), 0);
                break;
            case R.id.ll_coupon://代金点
                MyCashPointActivity.start(getActivity());
                break;
            case R.id.ll_business:      // 商家入口
                BusinessInActivity.start(getActivity());
                break;
        }
    }

    @OnClick(R.id.iv_head_circle)
    public void onViewClicked() {
    }


    @Subscribe
    public void onUserUpdateEvent(UserUpdateEvent event) {
        mRefresh.autoRefresh();
    }

    @Override
    public void setUserInfo(UserInfo userInfo) {
        UserMgr.getInstance().saveUser(userInfo);
        this.loginUser = userInfo;
        ViewUtil.setRefreshComplete(mRefresh, true);
        setUi();
    }

    @Override
    public void setAccountDetail(List<BalanceBean> balanceBeans, int totalPage) {

    }
}
